package com.kayden.blog.common.interceptor;

import com.kayden.blog.common.constant.Constants;
import com.kayden.blog.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        String userToken = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        log.info("从header中获取token:"+userToken);
        if (userToken == null){
            //拦截
            response.setStatus(401);//401:未登录
            return false;
        }
        //校验token是否合法
        Claims claims = JwtUtils.parseToken(userToken);
        if(claims == null){
            //拦截
            response.setStatus(401);
            return false;
        }
        return  true;
    }
}
